import numpy as np
import cv2 as cv
import scipy.io as scio
import matplotlib.pyplot as plt

def operation(image):
    image = image.T
    for i in range(820):
        image[i] =(np.abs(np.fft.ifft(image[i])))
    image = image.T
    return image



# 数据图像提取的操作;
datapath= "./spect.mat"

data = scio.loadmat(datapath)
# print(data.keys())
# 最后的输出的结果为:
# dict_keys(['__header__', '__version__', '__globals__', 'spect'])
DataSpect = data['spect']
# print(DataSpect)

# 使用矩阵进行仿射变换的过程;
r,g,b,d = cv.split(DataSpect)
h,w = r.shape[:2]

# 进行仿射变换中的平移变换；
MOne = np.float32([[1,0,0],[0,1,0]])
MTwo = np.float32([[1,0,-3],[0,1,0]])
MThree = np.float32([[1,0,-6],[0,1,0]])
MFour = np.float32([[1,0,-9],[0,1,0]])



# 下面使用Cv的warpAffine(img,M,(w,h))来进行平移的操作;
rMove = cv.warpAffine(r,MOne,(w,h))
gMove = cv.warpAffine(g,MTwo,(w,h))
bMove = cv.warpAffine(b,MThree,(w,h))
dMove = cv.warpAffine(d,MFour,(w,h))

# 进行傅立叶变换;
rMove = operation(rMove)
gMove = operation(gMove)
bMove = operation(bMove)
dMove = operation(dMove)

# 卷积内核;
fil = np.ones((3, 2))


rMove= cv.filter2D(rMove,-1,fil)
gMove= cv.filter2D(gMove,-1,fil)
bMove= cv.filter2D(bMove,-1,fil)
dMove= cv.filter2D(dMove,-1,fil)


# 使用np.log()进行运算;
result = (np.abs(rMove) + np.abs(gMove) + np.abs(bMove) + np.abs(dMove))/4
# result = cv.merge((rMove,gMove,bMove))
# rMove = cv.applyColorMap(rMove,cv.COLORMAP_AUTUMN)

rresult = np.ones((1024,820))
for i in range(1024):
   rresult[i] = result[i]

# 进行sobel蒜子进行运算:
SobelResult = cv.Sobel(rresult,cv.CV_64F,0,1)


# 进行闭运算的操作:
k = np.ones((5,5),np.uint8)
img_dilate = cv.dilate(rresult,k)
k = np.ones((2,5),np.uint8)
img_erode = cv.erode(img_dilate,k)


# 在提取一次边缘:
SobelResult = cv.Sobel(rresult,cv.CV_64F,1,0)


# 尝试来提取血管信息:
# r,g,b,d = cv.split(rresult)

# cv.imshow("result",20*rresult)

# 这几张图提取出表层的皮肤是完全没有问题的;
cv.imshow("Sobel",SobelResult)
cv.imshow("img_erode",img_erode)
cv.imshow("img_dilate",img_dilate)
# cv.imshow("r",rMove)
# cv.imshow("g",20*gMove)
# cv.imshow("b",20*bMove)
# cv.imshow("d",20*dMove)


#   最后一个实验的图最好不要用cv.imshow()来进行实现;
# cv.imshow("test",20*((rMove - gMove)*(rMove - gMove)/((rMove + gMove)*(rMove + gMove))))
cv.waitKey()
cv.destroyAllWindows()


"""
pre_picture = np.log((rMove - gMove)*(rMove - gMove)/((rMove + gMove)*(rMove + gMove)))
ord_picture = np.log((bMove - dMove)*(bMove - dMove)/((bMove + dMove)*(bMove + dMove)))
mid_picture = (pre_picture + ord_picture)/2
# print(result[1023])
plt.subplot(221)
plt.imshow(mid_picture)
plt.subplot(222)
plt.imshow(np.log((bMove - dMove)*(bMove - dMove)/((bMove + dMove)*(bMove + dMove))))

plt.subplot(223)
plt.imshow(np.log(rMove))
plt.subplot(224)
plt.imshow(np.log(gMove))
# plt.imshow(20*SobelResult)
# plt.imshow(np.log(np.abs(rMove + gMove + bMove + dMove - 4*rMove)))
# plt.imshow(DataSpect)
plt.show()
"""




